const {imgUrl} = getApp().globalData
import { mineApi } from "@/utils/request/api/mine";
Page({
  data: {
    imgUrl: imgUrl + 'mine/',
    active: 0,
    certifications: [], // 资质证书
    company: {},
    contracts: [],
    employees: [],
    preImageSrc: '',
    showPre: false
  },
  clickbtn(event) {
    let active = event.currentTarget.dataset.active;
    this.setData({
      active: active
    })
  },
  newliscense() {
    wx.navigateTo({
      url: '/pages/mine/myFirm/addLicense/addLicense',
    })
  },
  toAddContract() {
    wx.navigateTo({
      url: '/pages/mine/myFirm/addContract/addContract',
    })
  },
  toEditMainer(event) {
    wx.navigateTo({
      url: '/pages/mine/myFirm/editMainer/editMainer',
    })
  },
  toAddMainer() {
    wx.navigateTo({
      url: '/pages/mine/myFirm/addMainer/addMainer',
    })
  },
  handlePreImg(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      showPre: true,
      preImageSrc: this.data.certifications[index].file_url
    })
  },
  handlePreContract(event) {
    let src = event.currentTarget.dataset.src;
    this.setData({
      showPre: true,
      preImageSrc: src
    })
  },
  onClose() {
    this.setData({
      showPre: false,
    })
  }, 
  showMore(event) {
    let index = event.currentTarget.dataset.index;
    let certifications = JSON.parse(JSON.stringify(this.data.certifications));
    certifications.forEach(item => {
      item.isMore = false
    })
    certifications[index].isMore = !certifications[index].isMore
    this.setData({
      certifications: certifications
    })
  },
  hideMore() {
    let certifications = JSON.parse(JSON.stringify(this.data.certifications));
    certifications.forEach(item => {
      item.isMore = false
    })
    this.setData({
      certifications: certifications
    })
  },
  handleEditLiscense(event) {
    let index = event.currentTarget.dataset.index;
    let liscense = JSON.parse(JSON.stringify(this.data.certifications[index]));
    wx.navigateTo({
      url: '/pages/mine/myFirm/addLicense/addLicense',
      success: function(res) {
        res.eventChannel.emit('edit', { data: liscense });
      }
    });
  },
  handleEditContract(event) {
    let index = event.currentTarget.dataset.index;
    let contract = JSON.parse(JSON.stringify(this.data.contracts[index]));
    wx.navigateTo({
      url: '/pages/mine/myFirm/addContract/addContract',
      success: function(res) {
        res.eventChannel.emit('edit', { data: contract });
      }
    });
  },
  handleEditMainer(event) {
    let index = event.currentTarget.dataset.index;
    let employees = JSON.parse(JSON.stringify(this.data.employees[index]));
    console.log('employees === ', employees)
    wx.navigateTo({
      url: '/pages/mine/myFirm/addMainer/addMainer',
      success: function(res) {
        res.eventChannel.emit('edit', { data: employees });
      }
    });
  },
  onLoad(options) {
    
  },
  onReady() {
  },
  onShow() {
    mineApi.getCompanyInfo().then(res => {
      console.log('com === ', res)
      if(res.data.certifications.length > 0) {
        res.data.certifications.forEach(item => {
          item.isMore = false
        })
      }
      this.setData({
        certifications: res.data.certifications,
        company: res.data.company,
        contracts: res.data.contracts,
        employees: res.data.employees
      }, () => {
        wx.setStorageSync('company_id', res.data.certifications[0].company_id || '')
      })
    })
  },
  onHide() {
  },
  onUnload() {
  },
  onShareAppMessage() {
    return {
      title: '',
    };
  },
});